package com.grape.advice;

import com.grape.error.BusinessException;
import com.grape.result.R;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author fuhehuang
 * @email 2628279194@qq.com
 */
public class CommonControllerAdvice {


    private static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(CommonControllerAdvice.class);


    @ExceptionHandler(BusinessException.class)
    @ResponseBody
    public R handleBusinessException(BusinessException ex){
        return R.systemError(ex.getMessage(), null);
    }


    @ExceptionHandler(Exception.class)
    @ResponseBody
    public R handleDefaultException(Exception ex){
        ex.printStackTrace();//在控制台打印错误消息.
        return R.systemError(ex.getMessage(), null);
    }


    @ExceptionHandler(value = HttpRequestMethodNotSupportedException.class)
    public R handler(HttpRequestMethodNotSupportedException ex){
        ex.printStackTrace();
        String method = ex.getMethod();
        logger.error("请求方式错误：{}", ex.getMessage());
        return R.operationError("当前请求不支持" + method + "请求方式,", ex.getMessage(), ex.getMessage());
    }

}
